//Controller for the login page
function LoginCtrl($scope,kps,$location,$rootScope) {
    // Code for user interface
    $(function() {
        $('input').keyup(function(event) {
            event.preventDefault();
            if (event.which == 13) {
                $('#login').focus();
                $scope.login();
            }
        })
    });
    // Code for user interface end
    $scope.login=function() {
        var ulUser = kps.getUserByEmail($scope.newEmail);
        //Differing between adding a new user, and login operations for users registerd on this device
        if (ulUser instanceof YStamp.KaffipadUser) {
                //Differing between unlocking user and unlocking app
                var loginPromise=kps.login(ulUser, $scope.newPassword);
                loginPromise.then(
                function(message){
                    if($rootScope.locked){
                        $rootScope.locked=false;
                        $scope.userMessage("Application unlocked");
                    }
                    $scope.home();
                    ulUser.stampUser.values.name = $scope.newName;
                    $scope.userMessage(message, "response");
                    $(".lockedIcon").hide();
                    $(".appIcon").show();
                    $("#lockFunctions").show();
                    $scope.putCookie(ulUser.stampUser.values.email, kps.buildUserCookie(ulUser), 14);
                },
                function(reason){
                    $scope.userMessage("Invalid login information","error");
                })
        }
        else{
            if($rootScope.locked){
                $scope.userMessage("Login as a registerd user to unlock application", "error")
            }
            else{
                $scope.addUser();
            }
        }
    };
    //Building user from UI form
    $scope.buildUser = function() {
        var email=$scope.newEmail;
        var name=$scope.newName;
        if(_.isUndefined(name)||name==""){
            $scope.userMessage("User must have a name", "error")
        }
        else{
            var newUser = new YStamp.User(email,name);
            //Validates email format
            if (newUser.validateEmail()) {
                return new YStamp.KaffipadUser(newUser);
            }
            $scope.userMessage("Invalid email address", "error");
        }
    }
    $scope.addUser = function() {
        var kpUser=$scope.buildUser();
        //Tries to log in at Yast server:
        var loginPromise=kps.login(kpUser, $scope.newPassword);
        
        //Defines asynchronized behaviour for login attempt
        loginPromise.then(
        function(message){
            kps.addUser(kpUser);
//            
            //redirect
            $scope.home();
            
            $scope.userMessage("User("+kpUser.getEmail()+") added");
//            console.log("MainCtr():addUser():User added: "+message);
        },
        function(reason){
            $scope.userMessage("Could not add "+kpUser.getEmail()+". Is the input valid?");
//            console.log("MainCtr():addUser():User not added: "+reason);
        })
    }
};
LoginCtrl.$inject=['$scope','KaffipadService','$location','$rootScope'];